/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminMainFrame.java
 *
 * Created on 2011-4-13, 12:15:24
 */
package cs1555prj;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author sddys
 */
public class AdminMainFrame extends javax.swing.JFrame {

    private MyDataBase db = null;
    private boolean isCreateNewCustomer = true;
    private Calendar systime;
    private DefaultListModel listmodel3 = new DefaultListModel();
    private int option = 0;

    /** Creates new form AdminMainFrame */
    public AdminMainFrame(MyDataBase mdb) {
        initComponents();
        db = mdb;
        systime = Calendar.getInstance();
        updateSystemTime();
        //tab 3 
        list3.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        updateAllCustomerList();
        DefaultTableModel model = new DefaultTableModel() {

            @Override
            public boolean isCellEditable(int row, int column) {    //make cell uneditable
                return false;
            }
        };
        model.addColumn("Aucdtion id");
        model.addColumn("Name");
        model.addColumn("Seller");
        model.addColumn("Highest Bid");
        model.addColumn("Bidder");
        model.addColumn("Status");
        model.addColumn("Buyer");

        table3.setModel(model);
        table3.getColumnModel().getColumn(0).setPreferredWidth(5);//auction id
        table3.getColumnModel().getColumn(1).setPreferredWidth(60);//name
        table3.getColumnModel().getColumn(2).setPreferredWidth(5);//seller
        table3.getColumnModel().getColumn(3).setPreferredWidth(30);//bidding amount
        table3.getColumnModel().getColumn(4).setPreferredWidth(10);//bidder
        table3.getColumnModel().getColumn(5).setPreferredWidth(10);//status
        table3.getColumnModel().getColumn(6).setPreferredWidth(10);//buyer
        table3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        //tab 4 stat

        edMonth4.setText("1");
        edTop4.setText("5");
    }

    private void clearAllRows(JTable table) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        int numrows = model.getRowCount();
        for (int i = numrows - 1; i >= 0; i--) {
            model.removeRow(i);
        }
    }

    private void updateAllCustomerList() {
        list3.removeAll();
        listmodel3.addElement("All Customers");
        ResultSet rs = db.getAllCustomerLogin();
        try {
            while (rs.next()) {
                listmodel3.addElement(rs.getString("login"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(AdminMainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        list3.setModel(listmodel3);
        list3.setSelectedIndex(0);
    }

    /**
     * Get system time from datebase
     */
    private void updateSystemTime() {
        systime.setTime(db.getSystemTime());
        SimpleDateFormat dateformat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
        dateDisplay2.setText(dateformat.format(systime.getTime()));
        newDate2.setText(dateformat.format(systime.getTime()));
    }

    /**statistics for top k highest volume categories for the past x month*/
    private void statLeafCategory(int k, int x) {
        clearAllRows(table4);//clear the table first
        DefaultTableModel model4 = new DefaultTableModel() {

            @Override
            public boolean isCellEditable(int row, int column) {    //make cell uneditable
                return false;
            }
        };
        model4.addColumn("Leaf Category");
        model4.addColumn("Total Product Sold");
        table4.setModel(model4);
        table4.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        ResultSet rs = db.getStatForLeaf(k, x);
        if (rs == null) {
            return;
        }
        try {
            ArrayList<String> newrow = new ArrayList<String>();
            while (rs.next()) {
                newrow.clear();
                newrow.add(rs.getString("name"));
                newrow.add(rs.getString("itemsold"));

                ((DefaultTableModel) table4.getModel()).addRow(newrow.toArray());
            }
        } catch (SQLException ex) {
            Logger.getLogger(CustomerMainFrame.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Result handling fail\n!");
        }
    }

    /**statistics for top k most active bidder  for the past x month*/
    private void statBidder(int k, int x) {
        clearAllRows(table4);//clear the table first
        DefaultTableModel model4 = new DefaultTableModel() {

            @Override
            public boolean isCellEditable(int row, int column) {    //make cell uneditable
                return false;
            }
        };
        model4.addColumn("Bidder");
        model4.addColumn("Total Bid Count");
        table4.setModel(model4);
        table4.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        ResultSet rs = db.getStatForBidder(k, x);
        if (rs == null) {
            return;
        }
        try {
            ArrayList<String> newrow = new ArrayList<String>();
            while (rs.next()) {
                newrow.clear();
                newrow.add(rs.getString("bidder"));
                newrow.add(rs.getString("count"));

                ((DefaultTableModel) table4.getModel()).addRow(newrow.toArray());
            }
        } catch (SQLException ex) {
            Logger.getLogger(CustomerMainFrame.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Result handling fail\n!");
        }
    }

    /**statistics for top k most active bidder  for the past x month*/
    private void statBuyer(int k, int x) {
        clearAllRows(table4);//clear the table first
        DefaultTableModel model4 = new DefaultTableModel() {

            @Override
            public boolean isCellEditable(int row, int column) {    //make cell uneditable
                return false;
            }
        };
        model4.addColumn("Buyer");
        model4.addColumn("Total Amount");
        table4.setModel(model4);
        table4.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        ResultSet rs = db.getStatForBuyer(k, x);
        if (rs == null) {
            return;
        }
        try {
            ArrayList<String> newrow = new ArrayList<String>();
            while (rs.next()) {
                newrow.clear();
                newrow.add(rs.getString("buyer"));
                newrow.add(rs.getString("amount"));

                ((DefaultTableModel) table4.getModel()).addRow(newrow.toArray());
            }
        } catch (SQLException ex) {
            Logger.getLogger(CustomerMainFrame.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Result handling fail\n!");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TabPane = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        edName1 = new javax.swing.JTextField();
        edAddr1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        edEmail1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        edLogin1 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        edPwd1 = new javax.swing.JTextField();
        btnCreateUser = new javax.swing.JButton();
        btnCancel1 = new javax.swing.JButton();
        radioCustomer = new javax.swing.JRadioButton();
        radioAdmin = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        dateDisplay2 = new javax.swing.JLabel();
        btn5Sec2 = new javax.swing.JButton();
        btnCustomTime2 = new javax.swing.JButton();
        newDate2 = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        table3 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        list3 = new javax.swing.JList(listmodel3);
        jLabel6 = new javax.swing.JLabel();
        btnSearch3 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        table4 = new javax.swing.JTable();
        jLabel9 = new javax.swing.JLabel();
        edTop4 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        edMonth4 = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        radioLeaf4 = new javax.swing.JRadioButton();
        radioRoot4 = new javax.swing.JRadioButton();
        radioBidder4 = new javax.swing.JRadioButton();
        radioBuyer4 = new javax.swing.JRadioButton();
        btnStat4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cs1555prj.Cs1555PrjApp.class).getContext().getResourceMap(AdminMainFrame.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        TabPane.setName("TabPane"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        edName1.setText(resourceMap.getString("edName1.text")); // NOI18N
        edName1.setName("edName1"); // NOI18N

        edAddr1.setText(resourceMap.getString("edAddr1.text")); // NOI18N
        edAddr1.setName("edAddr1"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        edEmail1.setName("edEmail1"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        edLogin1.setName("edLogin1"); // NOI18N

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        edPwd1.setName("edPwd1"); // NOI18N

        btnCreateUser.setFont(resourceMap.getFont("btnCreateUser.font")); // NOI18N
        btnCreateUser.setForeground(resourceMap.getColor("btnCreateUser.foreground")); // NOI18N
        btnCreateUser.setText(resourceMap.getString("btnCreateUser.text")); // NOI18N
        btnCreateUser.setName("btnCreateUser"); // NOI18N
        btnCreateUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateUserActionPerformed(evt);
            }
        });

        btnCancel1.setForeground(resourceMap.getColor("btnCancel1.foreground")); // NOI18N
        btnCancel1.setText(resourceMap.getString("btnCancel1.text")); // NOI18N
        btnCancel1.setName("btnCancel1"); // NOI18N
        btnCancel1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancel1ActionPerformed(evt);
            }
        });

        radioCustomer.setFont(resourceMap.getFont("jRadioButton6.font")); // NOI18N
        radioCustomer.setSelected(true);
        radioCustomer.setText(resourceMap.getString("radioCustomer.text")); // NOI18N
        radioCustomer.setName("radioCustomer"); // NOI18N
        radioCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioCustomerActionPerformed(evt);
            }
        });

        radioAdmin.setFont(resourceMap.getFont("radioAdmin.font")); // NOI18N
        radioAdmin.setText(resourceMap.getString("radioAdmin.text")); // NOI18N
        radioAdmin.setName("radioAdmin"); // NOI18N
        radioAdmin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioAdminActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btnCreateUser, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(31, 31, 31)
                                .addComponent(btnCancel1, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addGap(10, 10, 10))
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(edName1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(edAddr1, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(edEmail1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(edLogin1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(edPwd1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(89, 89, 89)
                        .addComponent(radioCustomer)
                        .addGap(57, 57, 57)
                        .addComponent(radioAdmin)))
                .addContainerGap(127, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(edName1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(edAddr1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(edEmail1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(edLogin1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(edPwd1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(radioCustomer)
                    .addComponent(radioAdmin))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnCancel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCreateUser, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE))
                .addGap(64, 64, 64))
        );

        TabPane.addTab(resourceMap.getString("jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        dateDisplay2.setFont(resourceMap.getFont("dateDisplay2.font")); // NOI18N
        dateDisplay2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        dateDisplay2.setText(resourceMap.getString("dateDisplay2.text")); // NOI18N
        dateDisplay2.setName("dateDisplay2"); // NOI18N

        btn5Sec2.setText(resourceMap.getString("btn5Sec2.text")); // NOI18N
        btn5Sec2.setName("btn5Sec2"); // NOI18N
        btn5Sec2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn5Sec2ActionPerformed(evt);
            }
        });

        btnCustomTime2.setText(resourceMap.getString("btnCustomTime2.text")); // NOI18N
        btnCustomTime2.setName("btnCustomTime2"); // NOI18N
        btnCustomTime2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCustomTime2ActionPerformed(evt);
            }
        });

        newDate2.setFont(resourceMap.getFont("newDate2.font")); // NOI18N
        newDate2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        newDate2.setText(resourceMap.getString("newDate2.text")); // NOI18N
        newDate2.setName("newDate2"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(92, 92, 92)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(newDate2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
                    .addComponent(dateDisplay2, javax.swing.GroupLayout.PREFERRED_SIZE, 515, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(137, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(153, 153, 153)
                .addComponent(btn5Sec2, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 179, Short.MAX_VALUE)
                .addComponent(btnCustomTime2, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(182, 182, 182))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addComponent(dateDisplay2, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(65, 65, 65)
                .addComponent(newDate2, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(68, 68, 68)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCustomTime2, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn5Sec2, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(89, Short.MAX_VALUE))
        );

        TabPane.addTab(resourceMap.getString("jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        jPanel3.setName("jPanel3"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        table3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        table3.setName("table3"); // NOI18N
        jScrollPane1.setViewportView(table3);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        list3.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "All Customer" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        list3.setName("list3"); // NOI18N
        jScrollPane2.setViewportView(list3);

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        btnSearch3.setFont(resourceMap.getFont("btnSearch3.font")); // NOI18N
        btnSearch3.setForeground(resourceMap.getColor("btnSearch3.foreground")); // NOI18N
        btnSearch3.setText(resourceMap.getString("btnSearch3.text")); // NOI18N
        btnSearch3.setName("btnSearch3"); // NOI18N
        btnSearch3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearch3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(btnSearch3, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))
                    .addComponent(jLabel6)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 544, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnSearch3, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 477, Short.MAX_VALUE)))
        );

        TabPane.addTab(resourceMap.getString("jPanel3.TabConstraints.tabTitle"), jPanel3); // NOI18N

        jPanel4.setName("jPanel4"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        table4.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        table4.setName("table4"); // NOI18N
        jScrollPane3.setViewportView(table4);

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        edTop4.setText(resourceMap.getString("edTop4.text")); // NOI18N
        edTop4.setName("edTop4"); // NOI18N

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        edMonth4.setText(resourceMap.getString("edMonth4.text")); // NOI18N
        edMonth4.setName("edMonth4"); // NOI18N

        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        radioLeaf4.setSelected(true);
        radioLeaf4.setText(resourceMap.getString("radioLeaf4.text")); // NOI18N
        radioLeaf4.setName("radioLeaf4"); // NOI18N
        radioLeaf4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioLeaf4ActionPerformed(evt);
            }
        });

        radioRoot4.setText(resourceMap.getString("radioRoot4.text")); // NOI18N
        radioRoot4.setName("radioRoot4"); // NOI18N
        radioRoot4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioRoot4ActionPerformed(evt);
            }
        });

        radioBidder4.setText(resourceMap.getString("radioBidder4.text")); // NOI18N
        radioBidder4.setName("radioBidder4"); // NOI18N
        radioBidder4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioBidder4ActionPerformed(evt);
            }
        });

        radioBuyer4.setText(resourceMap.getString("radioBuyer4.text")); // NOI18N
        radioBuyer4.setName("radioBuyer4"); // NOI18N
        radioBuyer4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioBuyer4ActionPerformed(evt);
            }
        });

        btnStat4.setFont(resourceMap.getFont("btnStat4.font")); // NOI18N
        btnStat4.setText(resourceMap.getString("btnStat4.text")); // NOI18N
        btnStat4.setName("btnStat4"); // NOI18N
        btnStat4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStat4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(edTop4, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(radioLeaf4)
                    .addComponent(radioRoot4)
                    .addComponent(radioBidder4)
                    .addComponent(radioBuyer4)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(edMonth4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel11))
                    .addComponent(btnStat4, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 595, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 477, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(edTop4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(radioLeaf4)
                        .addGap(18, 18, 18)
                        .addComponent(radioRoot4)
                        .addGap(18, 18, 18)
                        .addComponent(radioBidder4)
                        .addGap(18, 18, 18)
                        .addComponent(radioBuyer4)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(edMonth4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnStat4, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );

        TabPane.addTab(resourceMap.getString("jPanel4.TabConstraints.tabTitle"), jPanel4); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TabPane)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TabPane, javax.swing.GroupLayout.DEFAULT_SIZE, 516, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        this.dispose();
        Cs1555PrjApp.getApplication().getMainFrame().setVisible(true);
    }//GEN-LAST:event_formWindowClosing

    private void btnCreateUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateUserActionPerformed
        if (edLogin1.getText().equals("") || edPwd1.getText().equals("")) {
            JOptionPane.showMessageDialog(null,
                    "Login and Password must not be null!");
            return;
        }
        if (db.isLoginExist(edLogin1.getText(), isCreateNewCustomer) != 0) {
            JOptionPane.showMessageDialog(null,
                    "The login you enter has already exist!\nPlease choose another one");
            edLogin1.setText("");
            return;
        }
        if (!db.createNewUser(edName1.getText(),
                edAddr1.getText(),
                edEmail1.getText(),
                edLogin1.getText(),
                edPwd1.getText(),
                isCreateNewCustomer)) {
            JOptionPane.showMessageDialog(null,
                    "Create new user fail, please try again");
        } else {//new user create succeed
            edName1.setText("");
            edAddr1.setText("");
            edEmail1.setText("");
            edLogin1.setText("");
            edPwd1.setText("");
        }
    }//GEN-LAST:event_btnCreateUserActionPerformed

    private void radioCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioCustomerActionPerformed
        isCreateNewCustomer = true;
        radioCustomer.setSelected(true);
        radioAdmin.setSelected(false);
    }//GEN-LAST:event_radioCustomerActionPerformed

    private void radioAdminActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioAdminActionPerformed
        isCreateNewCustomer = false;
        radioCustomer.setSelected(false);
        radioAdmin.setSelected(true);
    }//GEN-LAST:event_radioAdminActionPerformed

    private void btnCancel1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancel1ActionPerformed
        this.dispose();
        Cs1555PrjApp.getApplication().getMainFrame().setVisible(true);
    }//GEN-LAST:event_btnCancel1ActionPerformed

    private void btn5Sec2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn5Sec2ActionPerformed
        updateSystemTime();
        systime.add(Calendar.SECOND, 5);
        db.setSystemTime(systime);
        updateSystemTime();
    }//GEN-LAST:event_btn5Sec2ActionPerformed

    private void btnCustomTime2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCustomTime2ActionPerformed
        try {
            systime.setTime(db.getSystemTime());
            SimpleDateFormat dateformat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
            dateDisplay2.setText(dateformat.format(systime.getTime()));
            String str_date = newDate2.getText();
            DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
            Calendar newDate = Calendar.getInstance();
            newDate.setTime((Date) formatter.parse(str_date));
            if (!newDate.after((Calendar) systime)) {//if new date is not after current date
                JOptionPane.showMessageDialog(null,
                        "The date you enter must be greater than current time");
            } else {
                db.setSystemTime(systime);
            }
        } catch (ParseException e) {
            JOptionPane.showMessageDialog(null,
                    "Please Enter Date/Time formmated as MM/dd/yyyy hh:mm:ss");
        }
    }//GEN-LAST:event_btnCustomTime2ActionPerformed

    private void btnSearch3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearch3ActionPerformed
        String cus = (String) list3.getSelectedValue();
        if (cus.equals("All Customers")) {
            cus = "";
        }
        //do query to get data
        ResultSet rs = db.getAllProductsByCustomer(cus);
        if (rs == null) {
            System.out.println("get production by categories fail!\n");
        } else {//result return successfully
            clearAllRows(table3);
            try {
                ArrayList<String> newrow = new ArrayList<String>();

                while (rs.next()) {
                    newrow.clear();
                    newrow.add(Integer.toString(rs.getInt("auction_id")));
                    newrow.add(rs.getString("product1"));
                    newrow.add(rs.getString("seller1"));
                    newrow.add(rs.getString("max1"));
                    newrow.add(rs.getString("bidder1"));
                    newrow.add(rs.getString("status1"));
                    newrow.add(rs.getString("buyer1"));
                    ((DefaultTableModel) table3.getModel()).addRow(newrow.toArray());
                }
            } catch (SQLException ex) {
                Logger.getLogger(CustomerMainFrame.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Result handling fail\n!");
            }
        }
    }//GEN-LAST:event_btnSearch3ActionPerformed

    private void radioLeaf4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioLeaf4ActionPerformed
        option = 0;
        radioLeaf4.setSelected(true);
        radioRoot4.setSelected(false);
        radioBidder4.setSelected(false);
        radioBuyer4.setSelected(false);
    }//GEN-LAST:event_radioLeaf4ActionPerformed

    private void radioRoot4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioRoot4ActionPerformed
        option = 1;
        radioLeaf4.setSelected(false);
        radioRoot4.setSelected(true);
        radioBidder4.setSelected(false);
        radioBuyer4.setSelected(false);
    }//GEN-LAST:event_radioRoot4ActionPerformed

    private void radioBidder4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioBidder4ActionPerformed
        option = 2;
        radioLeaf4.setSelected(false);
        radioRoot4.setSelected(false);
        radioBidder4.setSelected(true);
        radioBuyer4.setSelected(false);
    }//GEN-LAST:event_radioBidder4ActionPerformed

    private void radioBuyer4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioBuyer4ActionPerformed
        option = 3;
        radioLeaf4.setSelected(false);
        radioRoot4.setSelected(false);
        radioBidder4.setSelected(false);
        radioBuyer4.setSelected(true);
    }//GEN-LAST:event_radioBuyer4ActionPerformed

    private void btnStat4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStat4ActionPerformed
        int top4, month4;
        try {
            top4 = Integer.parseInt(edTop4.getText());
            if (top4 <= 0) {
                JOptionPane.showMessageDialog(null, "Please only input positive integer number!");
                edTop4.setText("");
                return;
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(null, "Please only input integer number!");
            edTop4.setText("");
            return;
        }
        try {
            month4 = Integer.parseInt(edMonth4.getText());
            if (month4 <= 0) {
                JOptionPane.showMessageDialog(null, "Please only input positive integer number!");
                edMonth4.setText("");
                return;
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(null, "Please only input integer number!");
            edMonth4.setText("");
            return;
        }
        switch (option) {
            case 0: //leaf category count
                statLeafCategory(top4, month4);
                break;
            case 1: //root category count
                break;
            case 2: //bidder 
                statBidder(top4, month4);
                break;
            case 3: //buy amount
                statBuyer(top4, month4);
                break;
        }
    }//GEN-LAST:event_btnStat4ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane TabPane;
    private javax.swing.JButton btn5Sec2;
    private javax.swing.JButton btnCancel1;
    private javax.swing.JButton btnCreateUser;
    private javax.swing.JButton btnCustomTime2;
    private javax.swing.JButton btnSearch3;
    private javax.swing.JButton btnStat4;
    private javax.swing.JLabel dateDisplay2;
    private javax.swing.JTextField edAddr1;
    private javax.swing.JTextField edEmail1;
    private javax.swing.JTextField edLogin1;
    private javax.swing.JTextField edMonth4;
    private javax.swing.JTextField edName1;
    private javax.swing.JTextField edPwd1;
    private javax.swing.JTextField edTop4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList list3;
    private javax.swing.JTextField newDate2;
    private javax.swing.JRadioButton radioAdmin;
    private javax.swing.JRadioButton radioBidder4;
    private javax.swing.JRadioButton radioBuyer4;
    private javax.swing.JRadioButton radioCustomer;
    private javax.swing.JRadioButton radioLeaf4;
    private javax.swing.JRadioButton radioRoot4;
    private javax.swing.JTable table3;
    private javax.swing.JTable table4;
    // End of variables declaration//GEN-END:variables
}
